package com.forever7776.life.core.service.impl.stat;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.forever7776.life.core.common.util.ResultVoUtil;
import com.forever7776.life.core.entity.stat.StatTweet;
import com.forever7776.life.core.mapper.praise.PraiseDetailTweetMapper;
import com.forever7776.life.core.mapper.stat.StatTweetMapper;
import com.forever7776.life.core.service.praise.IPraiseDetailTweetService;
import com.forever7776.life.core.service.stat.IStatTweetService;
import com.forever7776.life.remote.common.enums.StatusEnum;
import com.forever7776.life.remote.common.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 动弹统计表 服务实现类
 * </p>
 *
 * @author kz
 * @since 2018-04-11
 */
@Service
public class StatTweetServiceImpl extends ServiceImpl<StatTweetMapper, StatTweet> implements IStatTweetService {

    @Autowired
    private StatTweetMapper statTweetMapper;
    @Autowired
    private IPraiseDetailTweetService iPraiseDetailTweetService;
    @Autowired
    private PraiseDetailTweetMapper praiseDetailTweetMapper;

    /**
     * 根据动弹ID查询
     *
     * @param thirdIds
     * @return
     */
    @Override
    public List<StatTweet> selectByTweetIds(List<Integer> thirdIds) {
        return selectList(new EntityWrapper<StatTweet>().in("tweet_id", thirdIds).eq("status", StatusEnum.NORMAL.getKey()));
    }

    /**
     * 给动弹添加阅读统计
     *
     * @param tweetId
     * @return
     */
    @Override
    public ResultVO addReadStat(Integer tweetId) {
        statTweetMapper.addReadStatCount(tweetId);
        return ResultVoUtil.getSuccessResult("给动弹添加阅读数量成功~");
    }

    /**
     * 给动弹添加点赞统计
     *
     * @param userId
     * @param tweetId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO addPraiseStat(String userId, Integer tweetId) {
        if (iPraiseDetailTweetService.selectPraiseOneByUserIdAndTweetId(userId, tweetId) != null) {
            return ResultVoUtil.getFailResult("该动弹已经点赞~");
        }
        statTweetMapper.addPraiseStatCount(tweetId);
        iPraiseDetailTweetService.selectOneOrInsert(userId, tweetId);
        praiseDetailTweetMapper.updatePraiseTweetStatusPraise(userId, tweetId);
        return ResultVoUtil.getSuccessResult("给动弹添加点赞成功~");
    }

    /**
     * 给动弹取消点赞
     *
     * @param userId
     * @param tweetId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO cutPraiseStat(String userId, Integer tweetId) {
        if (iPraiseDetailTweetService.selectNotPraiseOneByUserIdAndTweetId(userId, tweetId) != null) {
            return ResultVoUtil.getFailResult("该动弹已经点赞~");
        }
        statTweetMapper.cutPraiseStatCount(tweetId);
        iPraiseDetailTweetService.selectOneOrInsert(userId, tweetId);
        praiseDetailTweetMapper.updatePraiseTweetStatusNotPraise(userId, tweetId);
        return ResultVoUtil.getSuccessResult("给动弹取消点赞成功~");
    }
}
